/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.system.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.ConfigDao;
import org.grayrabbit.cms.entity.Config;
import org.grayrabbit.cms.vo.system.ConfigBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Config</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:54:59 
 */
@Repository
public class ConfigDaoImpl extends GenericHibernateDao<Config, String>
		implements ConfigDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Config as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see ConfigDao#assembly(ConfigBean)
	 */
	public QueryCondition assembly(ConfigBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see ConfigDao#assemblyCount(ConfigBean)
	 */
	public QueryCondition assemblyCount(ConfigBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see ConfigDao#count(ConfigBean)
	 */
	public int count(ConfigBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see ConfigDao#assemblyNameQuery(ConfigBean)
	 */
	public QueryCondition assermblyNameQuery(ConfigBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see ConfigDao#assemblyNameQueryCount(ConfigBean)
	 */
	public QueryCondition assemblyNameQueryCount(ConfigBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see ConfigDao#findByPage(Page, ConfigBean)
	 */
	public List<Config> findByPage(Page<Config> page,
			ConfigBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(ConfigBean bean,QueryCondition qc){
		if(bean!=null){
			 if(StringUtils.isNotBlank(bean.getConfigType())){
				qc.add("And obj.configType = ? ", bean.getConfigType());
			}
            if(StringUtils.isNotBlank(bean.getConfigAttrKey())){
				qc.add("And obj.configAttrKey = ? ", bean.getConfigAttrKey());
			}
            if(StringUtils.isNotBlank(bean.getConfigAttrValue())){
				qc.add("And obj.configAttrValue like ? ","%"+ bean.getConfigAttrValue()+"%");
			}
            if(StringUtils.isNotBlank(bean.getMemo())){
				qc.add("And obj.memo like ? ", "%"+bean.getMemo()+"%");
			}
		}
	}
	
	public Config findConfigByAttrKey(String attrKey){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(attrKey)){
			qc.add("And obj.configAttrKey =? ",attrKey);
		}
		Config config=null;
		List<Config> configList=findByQc(qc);
		if((null!=configList)&&!configList.isEmpty()){
			config=configList.get(0);
		}
		 return config;
	}
	
	public List<Config> getConfigs(ConfigBean configBean){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(configBean,qc);
		List<Config> configList=findByQc(qc);
		return configList;
	}
}